﻿using Acme.BookStore.RoomTypes;
using Microsoft.AspNetCore.Mvc;
using SC.UserCenter.Infra;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.BookStore.ClientInfos
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class ClientInfoServices:ApplicationService, IClientInfoServices
    {
       IRepository<ClientInfo> _clientInfoRepository;

        public ClientInfoServices(IRepository<ClientInfo> clientInfoRepository)
        {
            _clientInfoRepository = clientInfoRepository;
        }
        /// <summary>
        /// 客户显示
        /// </summary>
        /// <param name="clienName">客户名</param>
        /// <param name="clienPhone">手机号</param>
        /// <param name="cardNo">卡号</param>
        /// <returns>返回任务</returns>
        [HttpGet("ClienShow")]
        public async Task<ResultDto<List<ClientInfoDTO>>> ClienShow(string? clienName, string? clienPhone, string? cardNo)
        {
            try
            {
                var list = await _clientInfoRepository.GetListAsync(x => x.IsDeleted == false);
                if (!string.IsNullOrEmpty(clienName))
                {
                    list = list.Where(x => x.ClienName == clienName).ToList();
                }
                if (!string.IsNullOrEmpty(clienPhone))
                {
                    list = list.Where(x => x.ClienPhone == clienPhone).ToList();
                }
                if (!string.IsNullOrEmpty(cardNo))
                {
                    list = list.Where(x => x.CardNo == cardNo).ToList();
                }
                var result = new ResultDto<List<ClientInfoDTO>>
                {
                    Data = list.Select(x => new ClientInfoDTO
                    {
                        ClienId = x.ClienId,
                        ClienName = x.ClienName,
                        ClienPhone = x.ClienPhone,
                        CardNo = x.CardNo,
                        ClienGrow = x.ClienGrow,
                        ClienRealcharge = x.ClienRealcharge,
                        ClienGive = x.ClienGive,
                        ClienIntegral = x.ClienIntegral,
                        ClienSex = x.ClienSex,
                        ClienBirthday = x.ClienBirthday,
                        ClienAddr = x.ClienAddr,
                        ClienFreeze=x.ClienFreeze,
                    }).ToList()
                };
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="clientInfoDTO">实体</param>
        /// <returns>返回任务</returns>
        [HttpPost("ClientAdd")]
        public async Task<ApiResult> ClientAdd(ClientInfoDTO clientInfoDTO)
        {
            try
            {
                var clientInfo = ObjectMapper.Map<ClientInfoDTO, ClientInfo>(clientInfoDTO);
                await _clientInfoRepository.InsertAsync(clientInfo);
                return ApiResult.Ok(data:clientInfo);
            }
            catch (Exception ex)
            {
                return ApiResult.Fail(ex.Message);
            }
        }


    }
} 
